
using System;
using System.Text;
using LastFMGenNHibernate.CEN;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
using NHibernate.Exceptions;
using LastFMGenNHibernate.EN.LastFM;

namespace LastFMGenNHibernate.CAD.LastFM
{
        public partial class ArtistaCAD : BasicCAD, IArtistaCAD
        {
        public ArtistaCAD() : base ()
        {
        }

        public ArtistaCAD(ISession sessionAux) : base (sessionAux)
        {
        }



        public ArtistaEN ReadOIDDefault (int id)
        {
                ArtistaEN artistaEN = null;

                try
                {
                        SesssionInitializeTransaction ();
                        artistaEN = (ArtistaEN)session.Load (typeof(ArtistaEN),
                                id

                                );
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in ArtistaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return artistaEN;
        }


        public int New_ (ArtistaEN artista)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        if (artista.Cancion != null) {
                                for (int i = 0; i < artista.Cancion.Count; i++)
                                        artista.Cancion [i] = (LastFMGenNHibernate.EN.LastFM.CancionEN)session.Load (typeof(LastFMGenNHibernate.EN.LastFM.CancionEN), artista.Cancion [i].Id);
                        }
                        if (artista.Grupo != null) {
                                artista.Grupo = (LastFMGenNHibernate.EN.LastFM.GrupoEN)session.Load (typeof(LastFMGenNHibernate.EN.LastFM.GrupoEN), artista.Grupo.Id);
                        }

                        session.Save (artista);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in ArtistaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return artista.Id;
        }

        public void AgregarEtiqueta (ArtistaEN artista)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        ArtistaEN artistaEN = (ArtistaEN)session.Load (typeof(ArtistaEN), artista.Id);

                        artistaEN.Etiqueta = artista.Etiqueta;


                        artistaEN.Nombre = artista.Nombre;


                        artistaEN.Valor = artista.Valor;

                        session.Update (artistaEN);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in ArtistaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }
        }
        public void Destroy (int id)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        ArtistaEN artistaEN = (ArtistaEN)session.Load (typeof(ArtistaEN),
                                id

                                );
                        session.Delete (artistaEN);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in ArtistaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }
        }

        public void Modify (ArtistaEN artista)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        ArtistaEN artistaEN = (ArtistaEN)session.Load (typeof(ArtistaEN), artista.Id);

                        artistaEN.Etiqueta = artista.Etiqueta;


                        artistaEN.Nombre = artista.Nombre;


                        artistaEN.Valor = artista.Valor;

                        session.Update (artistaEN);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in ArtistaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }
        }
        public System.Collections.Generic.IList<ArtistaEN> DevolverArtistas (int first, int size)
        {
                System.Collections.Generic.IList<ArtistaEN> result = null;
                try
                {
                        SesssionInitializeTransaction ();
                        if (size > 0)
                                result = session.CreateCriteria (typeof(ArtistaEN)).
                                         SetFirstResult (first).SetMaxResults (size).List<ArtistaEN>();
                        else
                                result = session.CreateCriteria (typeof(ArtistaEN)).List<ArtistaEN>();
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in ArtistaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return result;
        }
        }
}
